package me.jayramafisher.mcsurvival;

import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.SignChangeEvent;

//File: BlockEventHandler.java
//Description: recieves all block related events.
//Last date of modification (yyyymmdd): 20110716

/**
*The BlockEventHandler class is instantiated and registered by the SurvivalPlugin class. It receives and handles all block related events.
*/

public class BlockEventHandler extends BlockListener
{
    public SurvivalPlugin plugin;

    public BlockEventHandler(SurvivalPlugin instance)
    {
         plugin = instance;
         //
    }

    public void onBlockPlace(BlockPlaceEvent event)
    {
	    //
    }
	
    public void onBlockBreak(BlockBreakEvent event)
    {
        //
    }

    /**
     * Determines whether or not the block set for destruction due to fire is netherrack. If it is, the event continues, if not the event is cancelled.
     * @param event Block burn destruction event.
     */
    public void onBlockBurn(BlockBurnEvent event)
    {
        if(!event.getBlock().getType().equals(org.bukkit.Material.NETHERRACK))
        {
            event.setCancelled(true);
        }
    }

    /**
     * Determines whether or not the block on ignite is netherrack. If it is, the event continues, if not the event is cancelled.
     * @param event Block Ignition Event
     */

    public void onBlockIgnite(BlockIgniteEvent event)
    {
        if(!event.getBlock().getType().equals(org.bukkit.Material.NETHERRACK))
        {
            event.setCancelled(true);
        }
    }

    /**
     * Determines command lines used when player makes a sign.
     * Used for Market portion of plugin.
     * @param event Sign Change Event.
     */
    public void onSignChange(SignChangeEvent event)
    {
        //Retreive the player who changed this sign.
        Player p = event.getPlayer();

        //First line of the sign is the command line.
        String cmd = event.getLine(0).toLowerCase();

        //Market commands.
        if(cmd.contains("[market]"))
        {
            String cmd1 = event.getLine(1).toLowerCase();

            if(cmd1.contains("[create]"))
            {
                event.setLine(0, "MARKET PLACE");
                event.setLine(1, "");
                event.setLine(2, "     BY     ");
                event.setLine(3, p.getDisplayName());

                //Create market.


                p.sendMessage("Marketplace Sucessfully Created");
            }
            else if(cmd1.contains("[item]"))
            {

            }
            else if(cmd1.contains(""))
            {

            }
            else
            {
                p.sendMessage("Invalid Market Command");
            }

        }
    }

}
